package cn.wonderland.house.controller;

import cn.wonderland.house.service.ICouponTempService;
import cn.wonderland.house.domain.CouponTemp;
import cn.wonderland.house.query.CouponTempQuery;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/couponTemp")
public class CouponTempController {
    @Autowired
    public ICouponTempService couponTempService;


    /**
     * 保存和修改公用的
     *
     * @param couponTemp 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody CouponTemp couponTemp) {
        try {
            if (couponTemp.getCouponId() != null)
                couponTempService.update(couponTemp);
            else
                couponTempService.add(couponTemp);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            couponTempService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public CouponTemp get(@PathVariable("id") Long id) {
        return couponTempService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<CouponTemp> list() {

        return couponTempService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<CouponTemp> json(@RequestBody CouponTempQuery query) {
        return couponTempService.queryByPage(query);
    }
}
